package my.data.x.control;



import com.alibaba.datax.core.Engine;
import my.data.x.model.Job;
import my.data.x.result.Result;
import my.data.x.service.JobService;
import my.data.x.util.XSeqGen;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("job")
public class JobControl {
    @Autowired
    private JobService jobService;
    @Autowired
    private XSeqGen xSeqGen;
    @GetMapping("/")
    public Result jobs(){
        try{
            return Result.SUCC_RESULT(jobService.getJobService());
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        }
    }
    @GetMapping("/newId")
    public Result jobNewId(){
        try{
            return Result.SUCC_RESULT(jobService.genJobId());
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        }
    }
    @PutMapping("/")
    public Result newJob(@NotEmpty @RequestParam(name="name") String name, @RequestParam(name="config") String config ){

        try{
            return Result.SUCC_RESULT(jobService.newJob(name,config));
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        }
    }

    @DeleteMapping("/")
    public Result newJob(@Min(0) @RequestParam(name="jobid") Long jobid){
        try{
            return Result.SUCC_RESULT(jobService.deleteJob(jobid));
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        }
    }
    @GetMapping("/{jobid}/config")
    public String getJobConfig(@PathVariable("jobid")long jobid){
        try{
            return jobService.getJobConfig(jobid);
        }catch (Exception e){
            return e.getMessage();
        }
    }

    @GetMapping("/{jobid}/status")
    public String getJobStatus(@PathVariable("jobid")long jobid){
        try{
            return jobService.getJobStatus(jobid);
        }catch (Exception e){
            return e.getMessage();
        }
    }

    @PostMapping("/{jobid}/start")
    public Result jobStart(@PathVariable("jobid")long jobid){
        try{
            return Result.SUCC_RESULT(jobService.jobStart(jobid));
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        } catch (Throwable throwable) {
            return Result.SUCC_RESULT(0);
        }
    }

    @GetMapping("/test")
    public Result testJob(){
        String myargs="content://{\"content\":[{\"reader\":{\"name\":\"mysqlreader\",\"parameter\":{\"column\":[\"*\"],\"connection\":[{\"jdbcUrl\":[\"jdbc:mysql://localhost:3306/qsn?useUnicode=true&characterEncoding=utf8\"],\"table\":[\"q_student\"]}],\"password\":\"123456\",\"splitPk\":\"uuid\",\"username\":\"root\"}},\"writer\":{\"name\":\"mysqlwriter\",\"parameter\":{\"column\":[\"*\"],\"connection\":[{\"jdbcUrl\":\"jdbc:mysql://localhost:3306/datax?useUnicode=true&characterEncoding=utf8\",\"table\":[\"q_student\"]}],\"password\":\"123456\",\"preSql\":[\"truncate table q_student\"],\"session\":[\"set session sql_mode='ANSI'\"],\"username\":\"root\",\"writeMode\":\"insert\"}}}],\"setting\":{\"speed\":{\"channel\":5}}}";
        try{



            Engine.entryDirect(myargs,-1L);
            return Result.SUCC_RESULT(1);
        }catch (Exception e){
            return Result.ERR_RESULT(e);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return Result.ERR_RESULT();
        }
    }

}
